package main

import (
	"b2c-delivery-optimization/internal/algorithm"
	"b2c-delivery-optimization/internal/model"
	"log"
	"os"
	"time"
)

func main() {
	// 1. 创建输出目录
	outputDir := "./output"
	if err := os.MkdirAll(outputDir, 0755); err != nil {
		log.Fatalf("创建输出目录失败: %v", err)
	}

	// 2. 准备测试数据
	waveOrders := generateTestWaveOrders()
	metrics := generateTestMetrics()

	// 3. 创建验证器
	validator := algorithm.NewWaveAllocationValidator(outputDir)

	// 4. 生成所有图表和验证报告
	if err := validator.ValidateAllocation(waveOrders, metrics); err != nil {
		log.Fatalf("生成验证报告和图表失败: %v", err)
	}

	log.Println("所有图表已生成完成！请查看 output 目录下的文件：")
	log.Println("- wave_distribution.png (波次分布图)")
	log.Println("- priority_distribution.png (优先级分布图)")
	log.Println("- location_distribution.png (位置分布图)")
	log.Println("- algorithm_metrics.png (算法指标图)")
	log.Println("- validation_report.txt (验证报告)")
}

// 生成测试用的波次订单数据
func generateTestWaveOrders() [][]model.Order {
	waves := make([][]model.Order, 3) // 创建3个波次

	// 波次1
	waves[0] = []model.Order{
		{
			ID:       "O001",
			Priority: 1,
			Location: model.Location{Latitude: 30.1, Longitude: 120.1},
		},
		{
			ID:       "O002",
			Priority: 2,
			Location: model.Location{Latitude: 30.2, Longitude: 120.2},
		},
	}

	// 波次2
	waves[1] = []model.Order{
		{
			ID:       "O003",
			Priority: 1,
			Location: model.Location{Latitude: 30.3, Longitude: 120.3},
		},
		{
			ID:       "O004",
			Priority: 3,
			Location: model.Location{Latitude: 30.4, Longitude: 120.4},
		},
		{
			ID:       "O005",
			Priority: 2,
			Location: model.Location{Latitude: 30.5, Longitude: 120.5},
		},
	}

	// 波次3
	waves[2] = []model.Order{
		{
			ID:       "O006",
			Priority: 2,
			Location: model.Location{Latitude: 30.6, Longitude: 120.6},
		},
		{
			ID:       "O007",
			Priority: 1,
			Location: model.Location{Latitude: 30.7, Longitude: 120.7},
		},
	}

	return waves
}

// 生成测试用的算法指标数据
func generateTestMetrics() []algorithm.AlgorithmMetrics {
	now := time.Now()
	return []algorithm.AlgorithmMetrics{
		{
			Timestamp:     now.Add(-2 * time.Hour),
			PriorityScore: 0.75,
			LocationScore: 0.80,
			BalanceScore:  0.70,
		},
		{
			Timestamp:     now.Add(-1 * time.Hour),
			PriorityScore: 0.85,
			LocationScore: 0.85,
			BalanceScore:  0.80,
		},
		{
			Timestamp:     now,
			PriorityScore: 0.90,
			LocationScore: 0.88,
			BalanceScore:  0.85,
		},
	}
}
